﻿using System;
using System.Windows.Forms;
using System.Data;
using Gencode.PrsCommon.Data.Common;

namespace Gencode.PrsUtil.Controls
{
	public class PUMonthsComboBox : ComboBox
	{
		DataView m_months;

		public PUMonthsComboBox()
			: base()
		{
		}

		protected override void InitLayout()
		{
			base.InitLayout();
		}

		public void InitData(int yearid)
		{
			YearID = yearid;
			RefreshData();
		}

		public void RefreshData()
		{
			m_months = CommonData.GetMonths(YearID, true);
			DataSource = m_months;
			DisplayMember = "name";
		}

		protected override void Dispose(bool disposing)
		{
			if (m_months != null)
				m_months.Dispose();
			base.Dispose(disposing);
		}

		public int YearID
		{
			set;
			private get;
		}

		public int SelectedMonthTypwert
		{
			get
			{
				if (m_months != null &&
					m_months.Count > 0 &&
					m_months.Count > SelectedIndex &&
					SelectedIndex >= 0)
					return int.Parse(m_months[SelectedIndex].Row["typwert"].ToString());

				return 0;
			}
		}

		public double SelectedMonthTage
		{
			get
			{
				if (m_months != null &&
					m_months.Count > 0 &&
					m_months.Count > SelectedIndex &&
					SelectedIndex >= 0 &&
					m_months[SelectedIndex].Row["tage"].ToString() != string.Empty)
					return double.Parse(m_months[SelectedIndex].Row["tage"].ToString());

				return 0;
			}
		}

		public int SelectedMonthAtID
		{
			get
			{
				if (m_months != null &&
					m_months.Count > 0 &&
					m_months.Count > SelectedIndex &&
					SelectedIndex >= 0 &&
					m_months[SelectedIndex].Row["ATID"].ToString() != string.Empty)
					return int.Parse(m_months[SelectedIndex].Row["ATID"].ToString());

				return 0;
			}
			set
			{
				int ind = -1;

				if (m_months != null)
				{
					for (int i = 0; i < m_months.Count; i++)
					{
						try
						{
							if (int.Parse(m_months[i].Row["ATID"].ToString()) == value)
							{
								ind = i;
								break;
							}
						}
						catch { }
					}
				}

				SelectedIndex = ind;
			}
		}
	}
}
